package com.lovetemperature.action;

import java.util.List;

import com.lovetemperature.model.Shop;
import com.lovetemperature.model.Staff;
import com.lovetemperature.model.StaffStatus;
import com.lovetemperature.service.ShopService;
import com.lovetemperature.service.StaffService;
import com.opensymphony.xwork2.ActionSupport;

public class OneStaffAction extends ActionSupport {
	private StaffService staffService;
	private ShopService shopService;
	
	private String staffId;
	private Staff staff;
	private String shopId;
	
	private List<Shop> candidateShops;
	private StaffStatus[] candidateStatus = StaffStatus.values();
	private String status;
	
	private String password;
	
	private boolean newAdd;
	
	public String edit(){
		candidateShops = shopService.findAllShop();
		if(staffId!=null && !staffId.equalsIgnoreCase("")){	//已有员工
			staff = staffService.findById(new Long(staffId));
			newAdd = false;
		}else {	//新增员工
			staff = new Staff();
			newAdd = true;
		}
		staffId = null;
		return "success";
	}
	
	public String submit(){
		if(staffId!=null && !staffId.equalsIgnoreCase("")){	//已有员工
			finishShop();
			finishStatus();
			finishPassword();
			staffService.update(staff);
		}else {	//新增员工
			finishShop();
			finishStatus();
			finishPassword();
			staffService.addStaff(staff);
		}
		staffId = null;
		return "success";
	}
	
	private void finishPassword() {
		if(password!=null && !password.equalsIgnoreCase("")){
			staff.setPassword(password);
		}
	}

	private void finishShop(){
		Shop tmp = shopService.findById(new Long(shopId));
		staff.setWorkingShop(tmp);
	}
	
	private void finishStatus(){
		for(StaffStatus one : candidateStatus){
			if(one.toString().equals(status)){
				staff.setStatus(one);
				break;
			}
		}
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
	}

	public List<Shop> getCandidateShops() {
		return candidateShops;
	}

	public void setCandidateShops(List<Shop> candidateShops) {
		this.candidateShops = candidateShops;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public StaffStatus[] getCandidateStatus() {
		return candidateStatus;
	}

	public void setCandidateStatus(StaffStatus[] candidateStatus) {
		this.candidateStatus = candidateStatus;
	}

	public String getShopId() {
		return shopId;
	}

	public void setShopId(String shopId) {
		this.shopId = shopId;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public boolean isNewAdd() {
		return newAdd;
	}

	public void setNewAdd(boolean newAdd) {
		this.newAdd = newAdd;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
